class LocationController {
    def scaffold = Location

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.max)params.max = 10
        [ locationList: Location.list( params ) ]
    }

    def userLocations = {
        def currentUser = SCH.context.authentication
        def locationList = Location.findByPerson( currentUser )
    }

    def show = {
        [ location : Location.get( params.id ) ]
    }

    def delete = {
        def location = Location.get( params.id )
        if(location) {
            location.delete()
            flash.message = "Location ${params.id} deleted."
            redirect(action:list)
        }
        else {
            flash.message = "Location not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def location = Location.get( params.id )

        if(!location) {
                flash.message = "Location not found with id ${params.id}"
                redirect(action:list)
        }
        else {
            return [ location : location ]
        }
    }

    def update = {
        def location = Location.get( params.id )
        if(location) {
             location.properties = params
            if(location.save()) {
                flash.message = "Location ${params.id} updated."
                redirect(action:show,id:location.id)
            }
            else {
                render(view:'edit',model:[location:location])
            }
        }
        else {
            flash.message = "Location not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
      def location = new Location()
      location.properties = params
      return ['location':location]
    }

    def save = {
        def location = new Location()
        location.properties = params
        if(location.save()) {
            flash.message = "Location ${location.id} created."
            redirect(action:show,id:location.id)
        }
        else {
            render(view:'create',model:[location:location])
        }
    }

}